from bs4 import BeautifulSoup
import urllib
import urllib.request as req

# 获取网页信息
url = "https://www.sioe.cn/yingyong/yanse-rgb-16/"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
reqst = req.Request(url=url, headers=headers)
response = req.urlopen(reqst)
html = response.read()

bs = BeautifulSoup(html, "html.parser")
# print(bs)
table = bs.select(".area > table")
# print(table)
target = table[1].select("td")
# print(target[0].getText())
color = []
colorList = []
for i in range(len(target)):
    # print(target[i].getText(), end=", ")
    color.append(target[i].getText())
    if (i + 1) % 5 == 0:
        colorList.append(color)
        color = []
        # print()

# 输出list
# for i in colorList:
#     print(i)

colorDict = {}
for i in colorList:
    colorDict[i[2]] = i[3]

# 输出dict
# for k, v in colorDict.items():
#     print(f"{k}:{v}")

del colorDict["形象描述"]
# 格式化
for k, v in colorDict.items():
    print(f'<color name="{k}">{v}</color>')
